package com.xzr.demo.controller;

import com.xzr.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Author：徐志荣
 * @Date：2023/6/14 上午 11:33
 */


@RequestMapping("/test")  //路由注册
/*
@ResponseBody    //我返回的是一个数据,而非一个页面
@Controller      //让框架启动的时候加载当前类,  (只有加载了类,才能被使用)
*/

@RestController //ResponseBody+Controller
@Slf4j
public class TestController {

//    @RequestMapping(value = "/hi",method = RequestMethod.POST)
//   @PostMapping("/hi")
    @GetMapping("/hi")
    public String sayHi(String name,Integer num){
        return "hi, " + name + ", num = "+num;
    }
    //接受普通对象 ,不能接受 JSON对象
    @GetMapping("/showUser")
    public  String showUser(User user){
        return user.toString();
    }
    @GetMapping("/showTime")
    public  String showTime(@RequestParam(value = "t",required = false) String startTime,
                            @RequestParam("t2") String endTime){
        return "开始时间 : "+startTime + " | 结束时间 : " + endTime;
    }

    //接受 JSON 对象 (和第三方系统通讯时 常见的场景)
    @PostMapping("/showJsonUser")
    public String showJsonUser(@RequestBody User user){
        return user.toString();
    }

    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username") String username,
                        @PathVariable("password") String password) {
        return username + " - " + password;
    }

    //文件上传
    @RequestMapping("/upfile")
    public String upfile(@RequestPart("myfile")MultipartFile file) throws IOException {
        String path = "E:\\hc\\logs\\img.png";
        //保存文件
        file.transferTo(new File(path));
        return path;
    }
    //文件上传2
    @RequestMapping("/myupfile")
    public String myupfile(@RequestPart("myfile")MultipartFile file) throws IOException {
        //根目录
        String path = "E:\\hc\\logs\\";
        //根目录 + 唯一的文件名
        path += UUID.randomUUID().toString().replace("-","");
        //根目录 + 唯一的文件名+ 文件的后缀
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //保存文件
         file.transferTo(new File(path));
        return path;
    }
    //Spring MVC (Spring web)内置了 HttpServletRequest 和HttpServletResponse
    @GetMapping("/getparm")
    public String getParm(HttpServletRequest request){
        return request.getParameter("username");
    }

    //获取Cookie 第一种方法
    @RequestMapping("/getck")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie item:
             cookies) {
            log.error(item.getName() + " - " + item.getValue());
        }
        return "get cookies";
    }

    //获取Cookie 第二种方法
    @RequestMapping("/getck2")
    public String getCookie(@CookieValue("bite") String it){
        return "Cookie value = "+ it;
    }

    //获取头信息
    @RequestMapping("/getua")
    public String getUA(@RequestHeader("User-Agent") String userAgent){
        return userAgent;
    }

    //设置Session
    @RequestMapping("setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("username","789415");
        return "set Session Success";
    }

    //获取Session
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session!=null && session.getAttribute("username")!=null){
            return (String)session.getAttribute("username");
        }else{
            return "暂无Session信息";
        }
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(value = "username",required = false) String userinfo){
        return userinfo;
     }

     @RequestMapping("/respJson")
     public HashMap<String,String> respJson(){
        HashMap<String,String> map = new HashMap<>();
        map.put("Java","Spring ");
        map.put("MySQL","数据库 ");
        map.put("Redis","缓存 ");
        return map;
     }


}
